import 'dart:convert';
import 'dart:io';
import 'package:erp/global.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:photo_view/photo_view.dart';

class ImageSelector extends StatefulWidget {
  SelectResult result;
  bool enabled;

  @override
  State<StatefulWidget> createState() => ImageSelectorState();

  ImageSelector(this.result, {this.enabled = true});
}

class ImageSelectorState extends State<ImageSelector> {
  ImageProvider imageProvider;

  ImageSelectorState();

  @override
  void initState() {
    if (widget.result?.imageUrl?.isEmpty ?? true) {
      imageProvider = AssetImage("assets/pictures/picture.png");
    } else {
      imageProvider = NetworkImage(widget.result.imageUrl);
    }

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          color: Color(0xffeeeeee), borderRadius: BorderRadius.circular(10.0)),
      height: 200.0,
      child: Column(
        children: <Widget>[
          GestureDetector(
            child: Hero(
                tag: "image",
                child: Image(
                  image: imageProvider,
                  width: 150.0,
                  height: 150.0,
                )),
            onTap: () {
              Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => GestureDetector(
                      child: PhotoView(
                        imageProvider: imageProvider,
                        heroTag: "image",
                      ),
                    onTap: (){
                        Navigator.of(context).pop();
                    },
                  ),
                ),
              );
            },
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              FlatButton(
                  onPressed: !widget.enabled
                      ? null
                      : () async {
                          await buildImage(ImageSource.gallery);
                        },
                  child: Icon(Icons.image)),
              SizedBox(
                width: 20.0,
              ),
              FlatButton(
                  onPressed: !widget.enabled
                      ? null
                      : () async {
                          await buildImage(ImageSource.camera);
                        },
                  child: Icon(Icons.camera_alt)),
            ],
          )
        ],
      ),
    );
  }

  Future<void> buildImage(ImageSource source) async {
    var img = await ImagePicker.pickImage(source: source);
    if (img == null) {
      return;
    }

    widget.result._imageFile = img;

    widget.result.selected = true;
    setState(() {
      imageProvider = FileImage(widget.result._imageFile);
    });
  }
}

class SelectResult {
  bool selected;
  String imageUrl;
  File _imageFile;

  SelectResult({this.selected = false, this.imageUrl});

  Future<String> getBase64String() async {
    if (_imageFile == null) {
      var img = await rootBundle.load("assets/pictures/picture.png");
      var base64 = Base64Encoder().convert(img.buffer.asUint8List().toList());
      return base64;
    } else {
      return Base64Encoder().convert(await _imageFile.readAsBytes());
    }
  }
}